﻿namespace VisualSVN.Core
{
    using System;

    public class XorLicenseCodec : IEncoder, IDecoder
    {
        private byte[] xorKey;

        public XorLicenseCodec()
        {
            this.xorKey = new byte[] { 20, 0xb1, 0x7e, 0x2f, 0x31 };
        }

        public XorLicenseCodec(byte[] xorKey)
        {
            this.xorKey = new byte[] { 20, 0xb1, 0x7e, 0x2f, 0x31 };
            this.xorKey = xorKey;
        }

        public byte[] Decode(byte[] data)
        {
            return this.Encode(data);
        }

        public byte[] Encode(byte[] data)
        {
            byte[] buffer = new byte[data.Length];
            for (int i = 0; i < data.Length; i++)
            {
                buffer[i] = (byte) (data[i] ^ this.xorKey[i % this.xorKey.Length]);
            }
            return buffer;
        }
    }
}

